package com.vcredit.creditcat.redis;

import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.util.StringUtils;

/**
 * @description: ${todo} (描述这个文件的作用)
 * Project Name:credit_cat File Name:VcreditRedisCacheExtExt.java
 * Copyright (c) 2017/11/9 @author: 张新功 Inc.All Rights Reserved.
 */

public class VcreditRedisCacheExt implements Cache {

  private String name = "default";
  @Autowired
  private RedisServiceExt redisService;
  private long timeout;

  public VcreditRedisCacheExt() {
  }

  @Override
  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Override
  public Object getNativeCache() {
    return this.redisService;
  }

  @Override
  public ValueWrapper get(Object key) {
    if (StringUtils.isEmpty(key)) {
      return null;
    } else {
      String finalKey;
      if (key instanceof String) {
        finalKey = (String) key;
      } else {
        finalKey = key.toString();
      }

      Object object = this.redisService.get(finalKey);
      return object != null ? new SimpleValueWrapper(object) : null;
    }
  }

  @Override
  public <T> T get(Object key, Class<T> type) {
    if (!StringUtils.isEmpty(key) && null != type) {
      String finalKey;
      if (key instanceof String) {
        finalKey = (String) key;
      } else {
        finalKey = key.toString();
      }

      Object object = this.redisService.get(finalKey);
      return type != null && type.isInstance(object) && null != object ? (T) object : null;
    } else {
      return null;
    }
  }

  @Override
  public void put(Object key, Object value) {
    if (!StringUtils.isEmpty(key) && !StringUtils.isEmpty(value)) {
      String finalKey;
      if (key instanceof String) {
        finalKey = (String) key;
      } else {
        finalKey = key.toString();
      }

      if (!StringUtils.isEmpty(finalKey)) {
        this.redisService.put(finalKey, value, this.timeout);
      }

    }
  }

  @Override
  public ValueWrapper putIfAbsent(Object key, Object value) {
    if (!StringUtils.isEmpty(key) && !StringUtils.isEmpty(value)) {
      String finalKey;
      if (key instanceof String) {
        finalKey = (String) key;
      } else {
        finalKey = key.toString();
      }

      if (!StringUtils.isEmpty(finalKey)) {
        ValueWrapper valueWrapper = this.get(finalKey);
        if (null == valueWrapper) {
          this.put(finalKey, value);
        }

        return new SimpleValueWrapper(value);
      } else {
        return null;
      }
    } else {
      return null;
    }
  }

  @Override
  public void evict(Object key) {
    if (null != key) {
      String finalKey;
      if (key instanceof String) {
        finalKey = (String) key;
      } else {
        finalKey = key.toString();
      }

      if (!StringUtils.isEmpty(finalKey)) {
        if (!finalKey.contains("*")) {
          this.redisService.del(finalKey);
        } else {
          finalKey = finalKey.replace("*", "");
          Set<String> keys = redisService.keys(finalKey);
          for (String keyTemp : keys) {
            this.redisService.del(keyTemp);
          }
        }
      }

    }

  }

  @Override
  public void clear() {
  }

  public long getTimeout() {
    return this.timeout;
  }

  public void setTimeout(long timeout) {
    this.timeout = timeout;
  }
}
